package br.great.clientServer;

import java.io.File;
import java.io.IOException;
import br.great.config.GetCheminServer;
import br.great.DAO.DAO_Collection;
import br.great.beans.Collection;
import br.great.beans.User;
import br.great.functionalCore.FileSender;

/**
* PhotoMap
* Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
*/

/**
 * @author Developpement of PhotoMap
 */
public class CollectionSender {

    public static volatile  CollectionSender instace_CollectionSender = new  CollectionSender();
    
    /** The private constructor of  CollectionSender for singleton
    */
    private  CollectionSender(){    
    }
    
    /** The method for get the instance of the class  CollectionSender
    * @return  CollectionSender instace CollectionSender
    */
    public static  CollectionSender getInstance() {
        //if(instace CollectionSender==null)
           synchronized( CollectionSender.class) {
             if (instace_CollectionSender == null)
               instace_CollectionSender = new  CollectionSender();
           }
        return instace_CollectionSender;
    }
    // insert Collection in the database and store the symbol image in the server file system
    // return the Objet of Collection  with the ID of the Collection.
    public Collection insertCollection(Collection collection , User user){
        String URIImageSymbol = sendSymbolImage(collection, user.getLogin());
        collection.setURIImageSymbol(URIImageSymbol);
        collection.setURI(this.getCollectionURI(collection.getName(), user.getLogin()));
        DAO_Collection dc = DAO_Collection.getInstance();
        
        // insert the collection of user in
        collection.setID_Collection(dc.insertCollection(collection, user));
        return collection;
    }
    
    private String sendSymbolImage(Collection collection, String loginUser){
        //insert les codes de sauvgarder d'image. image doit s'appeller symbol'nomDeLaCollection.jpg' eg:symbolRome.jpg
        File outputFile = new File(GetCheminServer.serverfileAddress+"photos\\"+loginUser+"\\"+collection.getName()+"\\");
        outputFile.mkdirs();
        String photoPath = GetCheminServer.serverfileAddress+"photos\\"+loginUser+"\\"+collection.getName()+"\\symbol"+collection.getName()+".jpg";
        try {
            FileSender.senderFile(collection.getURIImageSymbol(),photoPath);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return "photos/"+loginUser+"/"+collection.getName()+"/symbol"+collection.getName()+".jpg";
    }
    
    private String getCollectionURI(String nameCollection,String loginUser){
        return GetCheminServer.serverAddress+"photos/"+loginUser+"/"+nameCollection+"/"+nameCollection+".owl";
    }
    
    public boolean sendCollectionOntology(StringBuffer owl, Collection collection){
        return true;
    }
    
//    public static void main(String[] args ){
//        Vector vTags = new Vector();
//        Vector vTrackPointList = new Vector();
//        CollectionSender dc =new CollectionSender();
////        DAO_Collection dc = DAO_Collection.getInstance();
//        User u = new User();
//        u.setLogin("hant");
//        Collection collection = new Collection();
//        collection.setBegin(Date.valueOf("1991-01-01"));
//        collection.setContentAnnotation("contentAnnotation_Collection");
//        collection.setEnd(Date.valueOf("1991-01-02"));
//        collection.setName("Collection1");
//        collection.setURI("org.steamer.photomap.collection1");
//        collection.setURIImageSymbol("P:\\Bureau\\projet_PHOTOMAP\\sydney\\garden.jpg");
//        
//        for (int k=1;k<5;k++){
//            Tag t = new Tag();
//            TrackPoint tp = new TrackPoint();
//            t.setTAG_VALUE("Tag"+k);
//            tp.setInstant(Timestamp.valueOf("1999-01-09 12:23:2"+k));
//            tp.setLatitude(k+k*k);
//            tp.setLongitude(k);
//            
//            vTags.add(t);
//            vTrackPointList.add(tp);  
//        }
//        collection.setTags(vTags);
//        collection.setTrackPointList(vTrackPointList);
//        
//        collection  = dc.insertCollection(collection, u);
//        System.out.println(collection.getURI());
//    }
}
